#include "send_recv.h"

void send_packet(struct send_ctx *sendCtx)
{

	char err_buf[100] = "";
	libnet_t *lib_net = NULL;
	libnet_ptag_t lib_t = 0;
	unsigned char src_mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
	unsigned char dst_mac[6] = {0x88,0x88,0x88,0x88,0x89,0x88};
	    
      
	lib_net = libnet_init(LIBNET_LINK_ADV, "enx00e04d6da7b3", err_buf);
	
	
	if(NULL == lib_net)  
	{  
	    perror("libnet_init");  
	    exit(-1);  
	}  


	// set sel = 1;
	lib_t = libnet_build_ethernet(
		(u_int8_t *)dst_mac,  
		(u_int8_t *)src_mac,  
		sendCtx->type,
		(u8 *) &(sendCtx->payload),	// payload 
		sendCtx->lens,  			// payload length
		lib_net,  
		0  
	);  
    
	int res = 0;  
	res = libnet_write(lib_net);
	if(-1 == res)  
	{  
	    perror("libnet_write");  
	    exit(-1);  
	}

	libnet_destroy(lib_net);     
	// printf("----ok-----\n");
} 

void start_sending_pkt(void){
	struct send_ctx *sendCtx;
	sendCtx                     = (struct send_ctx *)malloc(sizeof(struct send_ctx));
    sendCtx->type               = 0x9004;
	struct one_128b *one128b    = &(sendCtx->payload.one128b[0]);
	one128b->conf_type[0]   	= htons(0);
    one128b->conf_type[1]       = one128b->conf_type[0];
	one128b->pad                = htonl(0);
	one128b->data_1           	= htonl(0);
	one128b->data_2             = htonl(0);
	sendCtx->lens               = 50;
	send_packet(sendCtx);
}

void stop_sending_pkt(void){
	struct send_ctx *sendCtx;
	sendCtx                     = (struct send_ctx *)malloc(sizeof(struct send_ctx));
    sendCtx->type               = 0x9004;
	struct one_128b *one128b    = &(sendCtx->payload.one128b[0]);
	one128b->conf_type[0]   	= htons(1);
    one128b->conf_type[1]       = one128b->conf_type[0];
	one128b->pad                = htonl(0);
	one128b->data_1           	= htonl(0);
	one128b->data_2             = htonl(0);
	sendCtx->lens               = 50;
	send_packet(sendCtx);
}

void conf_sending_pkt(){
	int i;
	struct send_ctx *sendCtx;
	sendCtx = (struct send_ctx *)malloc(sizeof(struct send_ctx));
	struct one_128b *one128b    = &(sendCtx->payload.one128b[0]);
	one128b->conf_type[0]   	= htons(2);
    one128b->conf_type[1]       = one128b->conf_type[0];
	one128b->pad                = htonl(0x01020304);
	one128b->data_1           	= htonl(0x00050607);
	one128b->data_2             = htonl(0x08099001);
	for (i=1; i<64; i++){
		one128b             	= &(sendCtx->payload.one128b[i]);
		one128b->pad        	= htonl(0xffffffff);
    	one128b->conf_type[0]   = htons(0xffff);
    	one128b->conf_type[1]   = one128b->conf_type[0];
		one128b->data_1   		= htonl(0xffffffff);
		one128b->data_2       	= htonl(0xffffffff);
	}
    
	sendCtx->type           	= 0x9004;
	sendCtx->lens           	= 256;
	send_packet(sendCtx);

}


void set_internal_clks(int value){
	
	struct send_ctx *sendCtx;
	sendCtx = (struct send_ctx *)malloc(sizeof(struct send_ctx));
	struct one_128b *one128b    = &(sendCtx->payload.one128b[0]);
	one128b->conf_type[0]   	= htons(4);
    one128b->conf_type[1]       = one128b->conf_type[0];
	one128b->pad                = htonl(0);
	one128b->data_1           	= htonl(0);
	one128b->data_2             = htonl(value);
    
	sendCtx->type           	= 0x9004;
	sendCtx->lens           	= 50;
	send_packet(sendCtx);
}

